<template>
  <!-- 只有数据时显示，否则不显示，这样也不会短暂出现红色的undefined字样了 -->
  <view v-if="goods_info.goods_name" class="goods-detail-container">
    <swiper
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="1000"
      :circular="true"
    >
      <swiper-item v-for="(item, i) in goods_info.pics" :key="i">
        <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>

    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">¥{{ goods_info.goods_price }}</view>
      <!-- 商品信息主体区域 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{ goods_info.goods_name }}</view>
        <!-- 收藏 -->
        <view class="favorate">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="freight">快递：免运费</view>
    </view>

    <!-- 商品富文本, 节点nodes的值就是要渲染的html内容 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>

    <!-- 商品导航组件区域 -->
    <view class="goods_nav">
      <uni-goods-nav
        :fill="true"
        :options="options"
        :buttonGroup="buttonGroup"
        @click="onClick"
        @buttonClick="buttonClick"
      />
    </view>
  </view>
</template>

<script>
//使用vuex 1. 按需引入模块
import { mapState, mapMutations, mapGetters } from 'vuex';

export default {
  computed: {
    //使用vuex 2. 通过计算属性 mapState ,把 m_cart 模块的 cart,total 属性映射到组件中
    ...mapState('m_cart', []),
    ...mapMutations('m_cart', ['total'])
  },

  watch: {
    // 3.1. 监听 total 值的变化，通过第一个形参得到变化后的新值
    //     total(newVal) {
    //       // 3.2. 通过数组的 find() 方法，找到购物车按钮的配置对象
    //       const findResult = this.options.find((x) => x.text === '购物车')

    //       if (findResult) {
    //         // 3.3. 动态为购物车按钮的 info 属性赋值
    //         findResult.info = newVal
    //       }
    //     }

    total: {
      handler(newVal) {
        const findResult = this.options.find(x => x.text === '购物车');

        if (findResult) {
          // 3.3. 动态为购物车按钮的 info 属性赋值
          findResult.info = newVal;
        }
      },
      immediate: true
    }
  },

  data() {
    return {
      goods_info: {},
      options: [
        {
          icon: 'shop',
          text: '店铺'
        },
        {
          icon: 'cart',
          text: '购物车',
          info: 0
        }
      ],
      // 右侧按钮组的配置对象
      buttonGroup: [
        {
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        },
        {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }
      ]
    };
  },
  onLoad(options) {
    // console.log(options)
    const goods_id = options.goods_id;
    this.getGoodsDetail(goods_id);
  },

  methods: {
    ...mapMutations('m_cart', ['addToCart']),
    ...mapGetters('m_cart', ['total']),

    async getGoodsDetail(goods_id) {
      const { data: res } = await uni.$http.get('/api/public/v1/goods/detail', {
        goods_id
      }); //有糖
      if (res.meta.status !== 200) return uni.$showMsg();

      //对商品富文本中的img标签进行正则替换，使图片间没有空隙, 重新赋值保存
      res.message.goods_introduce = res.message.goods_introduce
        .replace(/<img /g, '<img style="display:block;"')
        .replace(/webp/g, 'jpg');

      this.goods_info = res.message;
    },

    //预览每张轮播图
    preview(i) {
      uni.previewImage({
        current: i,
        //原型urls:['','',''],本例中返回的新数组包含多个pics_big，我们以x表示
        urls: this.goods_info.pics.map(x => x.pics_big)
      });
    },

    onClick(e) {
      // console.log(e)
      if (e.content.text === '购物车') {
        uni.switchTab({
          url: '/pages/cart/cart'
        });
      }
    },

    buttonClick(e) {
      // console.log(e);
      // 1. 判断是否点击了 加入购物车 按钮
      if (e.content.text === '加入购物车') {
        // 2. 组织一个商品的信息对象
        const goods = {
          goods_id: this.goods_info.goods_id, // 商品的Id
          goods_name: this.goods_info.goods_name, // 商品的名称
          goods_price: this.goods_info.goods_price, // 商品的价格
          goods_count: 1, // 商品的数量
          goods_small_logo: this.goods_info.goods_small_logo, // 商品的图片
          goods_state: true // 商品的勾选状态
        };

        // 3. 通过 this 调用映射过来的 addToCart 方法，把商品信息对象存储到购物车中
        this.addToCart(goods);
      }
    }
  }
};
</script>

<style lang="scss">
.goods-detail-container {
  padding-bottom: 50px;
}
swiper {
  height: 750rpx;
  image {
    width: 100%;
    height: 100%;
  }
}

.goods-info-box {
  padding: 10px;
  padding-right: 0;

  .price {
    color: #c00000;
    font-size: 18px;
    margin: 10px 0;
  }
  .goods-info-body {
    display: flex;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
      margin-right: 10px;
    }
  }
  .favorate {
    width: 120px;
    font-size: 12px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border-left: 1px solid #dddddd;
    color: gray;
  }
  .freight {
    font-size: 12px;
    color: gray;
    margin: 10px 0;
  }
}

.goods_nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
</style>
